package io.renren.common.utils.PayUtils.WxPay.weixin.util;

import io.renren.common.utils.PayUtils.WxPay.weixin.ClientRequestHandler;
import io.renren.common.utils.PayUtils.WxPay.weixin.PrepayIdRequestHandler;
import org.jdom.JDOMException;
import org.json.JSONException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class WXUtil {

	/**
	 *
	 * @param body 商品描述
	 * @param detail 商品详情
	 * @param total_fee 总金额
	 * @param out_trade_no
	 * @return
	 */
	public static Map<String, Object> getResInf(String body, String detail, String total_fee ,
												String out_trade_no, HttpServletRequest request, HttpServletResponse response) throws IOException, JDOMException, JSONException {
		//返给客户端的参数集合
		Map<String, Object> resInfo = new HashMap<>();


		PrepayIdRequestHandler prepayReqHandler = new PrepayIdRequestHandler(request, response);//获取prepayid的请求类
		ClientRequestHandler clientHandler = new ClientRequestHandler(request, response);//返回客户端支付参数的请求类

		int retcode;  //请求结果代号
		String retmsg = "";  //请求结果说明
		String noncestr = WXUtil.getNonceStr();
		String timestamp = WXUtil.getTimeStamp();

		//---------------设置获取prepayid支付参数 开始------------------------
		prepayReqHandler.setParameter("appid", ConstantUtil.APP_ID);  //应用ID
		prepayReqHandler.setParameter("mch_id", ConstantUtil.PARTNER); //商户号
		prepayReqHandler.setParameter("nonce_str", noncestr); //随机字符串
		prepayReqHandler.setParameter("body", body); //商品描述
		prepayReqHandler.setParameter("detail", detail); //商品详情
		prepayReqHandler.setParameter("out_trade_no", out_trade_no); //商家订单号
		prepayReqHandler.setParameter("total_fee", total_fee); //商品总金额,以分为单位
		prepayReqHandler.setParameter("spbill_create_ip", request.getRemoteAddr()); //终端IP
		prepayReqHandler.setParameter("notify_url", ConstantUtil.NOTIFY_URL); //通知地址
		prepayReqHandler.setParameter("trade_type", "APP"); //交易类型
		String sign = prepayReqHandler.createMD5Sign(); //生成MD5签名参数
		prepayReqHandler.setParameter("sign", sign); //签名
		//---------------设置获取prepayid支付参数 结束------------------------

		String gateUrl = ConstantUtil.GATEURL;
		prepayReqHandler.setGateUrl(gateUrl);
		//获取prepayId
		String prepayid = prepayReqHandler.sendPrepay();
		//吐回给客户端的参数
		if (null != prepayid && !"".equals(prepayid)) {
			//输出参数列表
			clientHandler.setParameter("appid", ConstantUtil.APP_ID);
			clientHandler.setParameter("partnerid", ConstantUtil.PARTNER);
			clientHandler.setParameter("prepayid", prepayid);
			clientHandler.setParameter("package", "Sign=WXPay");
			clientHandler.setParameter("noncestr", noncestr);
			clientHandler.setParameter("timestamp", timestamp);
			sign = clientHandler.createMD5Sign(); //生成签名
			//---------拼接返回参数 开始----------
			resInfo.put("appid", ConstantUtil.APP_ID);
			resInfo.put("partnerid", ConstantUtil.PARTNER);
			resInfo.put("prepayid", prepayid);
			resInfo.put("package", "Sign=WXPay");
			resInfo.put("noncestr", noncestr);
			resInfo.put("timestamp", timestamp);
			resInfo.put("sign", sign);
			//---------拼接返回参数 结束----------
			retcode = 1;
			retmsg = "OK";
		} else {
			retcode = -2;
			retmsg = "错误：获取prepayId失败";
		}
		resInfo.put("retcode", retcode);
		resInfo.put("retmsg", retmsg);



		return resInfo;
	}

	public static String getNonceStr() {
		Random random = new Random();
		return MD5Util.MD5Encode(String.valueOf(random.nextInt(10000)), "GBK");
	}

	public static String getTimeStamp() {
		return String.valueOf(System.currentTimeMillis() / 1000);
	}
}
